{layout name="common/base" }
<style type="text/css">
    .layui-table-cell {
        height: auto;
        line-height: 28px;
    }
</style>
<table class="layui-hide" id="currentTableId" lay-filter="tooFilter"></table>

<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"> 添加商品 </button>
    </div>
</script>

<script type="text/html" id="changeSort">
    <input type="text" value="{{d.sort}}" data-id="{{d.id}}" class="layui-input changeSort">
</script>
<script type="text/html" id="hot">
    {{# if(d.is_hot == 0){ }}否
    {{# } else if(d.type == 1){ }}热门
    {{# } }}
</script>
<script type="text/html" id="goodscategory">
    <span>{{ d.goodscategory.name}}</span>
</script>
<script type="text/html" id="image">
    <img width="40px" height="40px" onclick="showimg(this)" src="<?php echo config('setting.domain_prefix').config('setting.image_prefix');?>{{d.image}}">
</script>
<script type="text/html" id="switchISOver">
    <input type="checkbox" name="is_over" value="{{d.id}}" lay-skin="switch" lay-text="是|否" lay-filter="switchISOver" {{ d.is_over == 0 ? '' : 'checked' }}>
</script>
<script type="text/html" id="switchISrecommend">
    <input type="checkbox" name="is_recommend" value="{{d.id}}" lay-skin="switch" lay-text="推荐|否" lay-filter="switchISrecommend" {{ d.is_recommend == 0 ? 'checked' : '' }}>
</script>
<script type="text/html" id="switchStatus">
    <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="switchStatus" {{ d.status == 1 ? 'checked' : '' }}>
</script>



<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-xs data-count-edit edit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete delete" lay-event="delete" data-id="{d.id}">删除</a>
</script>

<script>
    layui.use(['form', 'table', 'util'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            util = layui.util;
        table.render({
            elem: '#currentTableId',
            url: '/admin/store.goods/lstajax',
            toolbar: '#toolbarHead',
            cols: [[
                /*{type: "checkbox", width: 50, fixed: "left"},*/
                {field: 'id', width: 100, title: 'ID'},
                {field: 'name', width: 200, title: '商品名'},
                {field: 'image', width: 150, title: '商品图', templet: '#image'},
                {field: 'type', width: 150, title: '商品类型'},
                //{field: 'goodscategory', width: 100, title: '所属分类', templet: '#goodscategory'},
                {field: 'sell_price', width: 150, title: '售价'},
                {field: 'sales', width: 150, title: '销量'},
                {field: 'stock', width: 150, title: '库存'},
                {field: 'sort', width: 100, title: '排序', edit: 'text'},
                {field: 'is_hot', width: 100, title: '热门', templet: '#hot'},
                {field: 'status', width: 100, title: '状态', templet: '#switchStatus'},
                {field: 'create_time', width: 180, title: '创建时间', sort: true},
                {title: '操作', minWidth: 150, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]],
            limit: 10,
            page:{
                elem: 'page',
                layout: ['prev', 'page', 'next', 'last','skip'], //自定义分页布局
                groups: 5, //只显示 5 个连续页码
                prev: '上一页',
                next: '下一页',
                first: 0 //不显示首页
            },
            loading:true

        });

        //头工具栏事件
        table.on('toolbar(tooFilter)', function(obj){
            //var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    var index = layer.open({
                        title: '添加商品',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/admin/store.goods/add'
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
        });

        table.on('tool(tooFilter)', function(obj){
            var data = obj.data;
            console.log(data);
            switch(obj.event){
                case 'edit':
                    var index = layer.open({
                        title: '编辑商品',
                        type: 2,
                        shade: 0.2,
                        maxmin:true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '/admin/store.goods/edit/id/' + data.id
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    break;
                case 'delete':
                    layer.confirm('真的删除么?',{icon:3, title:'提示'}, function (index) {
                        $.ajax({
                            url: '/admin/store.goods/del/id/' + data.id,
                            type: 'GET',
                            success: function(res) {
                                //window.location.reload();
                                if(res.code == 1){
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                    }, function(){
                                        obj.del();
                                        layer.close(index);
                                    });
                                }else{
                                    layer.msg(res.msg, {
                                        icon: 1,
                                        time: 1000 //2秒关闭（如果不配置，默认是3秒）
                                    });
                                }
                            },
                            error: function(res) {
                                //return   err(res)
                            }
                        });
                    });
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            }
        });

        //监听单元格编辑
        table.on('edit(tooFilter)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            //layer.msg('[ID: '+ data.id +'] ' + field + ' 字段更改值为：'+ util.escape(value));
            console.log(data.id);
            console.log(field);
            console.log(util.escape(value));
            $.ajax({
                url: '{:url("admin/store.goods/change")}?id=' + data.id + '&field=' + field + '&value=' + value,
                success: function(res){
                    layer.msg(res.msg);
                    /*if(res.status == 1) {
                        window.location.reload();
                    } else {
                        layer.msg(res.msg);
                    }*/
                }
            });
        });

        form.on('switch(switchShipping)', function(obj){
            var id = this.value;
            var is_free_shipping = obj.elem.checked ? 0 : 1;
            console.log(id);
            console.log(is_free_shipping);
            $.ajax({
                url: '{:url("admin/store.goods/switchShipping")}?id=' + id + '&is_free_shipping=' + is_free_shipping,
                success: function(res){
                    layer.msg(res.msg);
                    /*if(res.code == 1) {
                        window.location.reload();
                    } else {
                        layer.msg(res.msg);
                    }*/
                }
            });
            return false;
        });
        form.on('switch(switchISOver)', function(obj){
            var id = this.value;
            var is_over = obj.elem.checked ? 0 : 1;
            console.log(id);
            console.log(is_over);
            $.ajax({
                url: '{:url("admin/store.goods/switchISOver")}?id=' + id + '&is_over=' + is_over,
                success: function(res){
                    layer.msg(res.msg);
                    /*if(res.code == 1) {
                        window.location.reload();
                    } else {
                        layer.msg(res.msg);
                    }*/
                }
            });
            return false;
        });
        form.on('switch(switchISrecommend)', function(obj){
            var id = this.value;
            var is_recommend = obj.elem.checked ? 0 : 1;
            console.log(id);
            console.log(is_recommend);
            $.ajax({
                url: '{:url("admin/store.goods/switchISrecommend")}?id=' + id + '&is_recommend=' + is_recommend,
                success: function(res){
                    layer.msg(res.msg);
                    /*if(res.code == 1) {
                        window.location.reload();
                    } else {
                        layer.msg(res.msg);
                    }*/
                }
            });
            return false;
        });
        form.on('switch(switchStatus)', function(obj){
            var id = this.value;
            var status = obj.elem.checked ? 1 : 0;
            console.log(id);
            console.log(status);
            $.ajax({
                url: '{:url("admin/store.goods/switchStatus")}?id=' + id + '&status=' + status,
                success: function(res){
                    layer.msg(res.msg);
                    /*if(res.code == 1) {
                        window.location.reload();
                    } else {
                        layer.msg(res.msg);
                    }*/
                }
            });
            return false;
        });

    });

    /*查看图片*/
    function showimg(url) {
        var img_infor = "<img src='" + url.src + "'  width='400' height='400' />";
        layer.open({
            type: 1,
            closeBtn: 1,
            shade: false,
            title: false, //不显示标题
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: false,
            area:['400px','400px'],
            content: img_infor //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响

        });
    }

</script>